package com.droid.faces;

import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.droid.persistor.dao.Categoria;
import com.droid.persistor.dao.Gasto;
import com.droid.persistor.dao.Objetivo;
import com.droid.persistor.dao.Recordatorio;
import com.droid.persistor.dao.Tipo_Gasto;
import com.droid.persistor.dao.Usuario;
import com.droid.persistor.facade.PersistorFacade;
import com.droid.users.facade.UsuarioFacade;
import com.droid.faces.UserHolder;

public class LoginRequest {
	private String email;
	private String password;
	private String nombre;
	private String apellido;

	// @Current
	// private UserHolder uh = new UserHolder();
	private UserHolder uh;

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	
	public String getApellido() {
		return apellido;
	}

	public void setApellido(String apellido) {
		this.apellido = apellido;
	}

	public String getUsername() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public void setUsername(String username) {
		this.email = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login() {

		UsuarioFacade usrf = new UsuarioFacade();
		String msg=usrf.login(email, password);

		if (msg.equalsIgnoreCase("loginOk")) {
//			UserHolder uh = new UserHolder();
//			uh.setCurrentUser(new User(email, password));
			return "loggedIn";
		} else {
			 if (msg.equalsIgnoreCase("CambiarPass")){
				 return "cambiarPass";
			 } else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR,
					"Usuario o password invalidos", null));
			return null;
			 }
		}
	}

	public String Cambiar_Pass() {
		UsuarioFacade usrf = new UsuarioFacade();
		String msg=null;
		if (usrf.CambiarPass(this.email, this.password).equalsIgnoreCase("Cambio correcto")){
		msg="cancelar";
		}
		return msg;
	}	
		
	public String alta() {
		UsuarioFacade usrf = new UsuarioFacade();

		String msgError;
		password="dummy";

		msgError = usrf.altaUsuario(this.email, this.password, this.nombre, this.apellido);

		if (msgError.equalsIgnoreCase("OK"))
		// if (username.equals("u1") && password.equals("p1"))
		{
//			UserHolder uh = new UserHolder();
//			uh.setCurrentUser(new User(email, password));
			return "loggedIn";
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, msgError, null));
			return null;
		}

	}

	public ArrayList<Gasto> getUsuarioGastosDB() {

		PersistorFacade pFacade = new PersistorFacade();
		ArrayList<Gasto> resp = pFacade.obtenerUsuarioGastos(email);
		return resp;

	}

	public String logout() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
		.getExternalContext().getSession(false);
		session.invalidate();
		return "login";
		} 
	
}
